<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>4.2. The theory of caching</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="prev" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="next" href="zend.cache.frontends.html" title="4.3. Zend_Cache frontends">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="subsection" href="zend.cache.theory.html#zend.cache.factory" title="4.2.1. The Zend_Cache factory method">
<link rel="subsection" href="zend.cache.theory.html#zend.cache.tags" title="4.2.2. Tagging records">
<link rel="subsection" href="zend.cache.theory.html#zend.cache.clean" title="4.2.3. Cleaning the cache">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">4.2. The theory of caching</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.cache.html">Prev</a> </td>
<th width="60%" align="center">Chapter 4. Zend_Cache</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.cache.frontends.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.cache.theory"></a>4.2. The theory of caching</h2></div></div></div>
<p>
        There are three key concepts in Zend_Cache. One is the unique identifier (a string) that is used
        to identify cache records. The second one is the <code class="code">'lifetime'</code> directive as seen in the
        examples; it defines for how long the cached resource is considered 'fresh'. The third key concept
        is conditional execution so that parts of your code can be skipped entirely, boosting performance.
        The main frontend function (eg. <code class="code">Zend_Cache_Core::get()</code>) is always designed to return
        false for a cache miss if that makes sense for the nature of a frontend. That enables end-users to
        wrap parts of the code they would like to cache (and skip) in <code class="code">if(){ ... }</code> statements where
        the condition is a Zend_Cache method itself. On the end if these blocks you must save what you've
        generated, however (eg. <code class="code">Zend_Cache_Core::save()</code>).
    </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        The conditional execution design of your generating code is not necessary in some frontends
        (<code class="code">Function</code>, for an example) when the whole logic is implemented inside the frontend.
    </p></td></tr>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        'Cache hit' is a term for a condition when a cache record is found, is valid and is 'fresh' (in other
        words hasn't expired yet). 'Cache miss' is everything else. When a cache miss happens, you must
        generate your data (as you would normally do) and have it cached. When you have a cache hit, on the
        other hand, the backend automatically fetches the record from cache transparently.
    </p></td></tr>
</table></div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.cache.factory"></a>4.2.1. The <code class="code">Zend_Cache</code> factory method</h3></div></div></div>
<p>
            A good way to build a usable instance of a <code class="code">Zend_Cache</code> Frontend is given
            in the following example :
        </p>
<pre class="programlisting">&lt;?php
# We "load" the Zend_Cache factory
require 'Zend/Cache.php';

# We choose a backend (for example 'File' or 'Sqlite'...)
$backendName = '[...]';

# We choose a frontend (for example 'Core', 'Output', 'Page'...)
$frontendName = '[...]';

# We set an array of options for the choosen frontend
$frontendOptions = array([...]);

# We set an array of options for the choosen backend
$backendOptions = array([...]);

# We make the good instance
# (of course, the two last arguments are optional)
$cache = Zend_Cache::factory($frontendName, $backendName, $frontendOptions, $backendOptions);
        </pre>
<p>
            In the following examples we will assume that the <code class="code">$cache</code> variable holds a
            valid, instantiated frontend as shown and that you understand how to pass parameters
            to your chosen backends.
        </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
            Always use <code class="code">Zend_Cache::factory()</code> to get frontend instances. Instantiating
            frontends and backends yourself will not work as expected.
        </p></td></tr>
</table></div>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.cache.tags"></a>4.2.2. Tagging records</h3></div></div></div>
<p>
            Tags are a way to categorize cache records. When you save a cache with the <code class="code">save()</code>
            method, you can set an array of tags to apply for this record. Then you will be
            able to clean all cache records tagged with a given tag (or tags):
        </p>
<pre class="programlisting">&lt;?php
$cache-&gt;save($huge_data, 'myUniqueID', array('tagA', 'tagB', 'tagC'));
        </pre>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
            note than the <code class="code">save()</code> method accepts an optional fourth argument : <code class="code">$specificLifetime</code>
            (if != false, it sets a specific lifetime for this particular cache record)
        </p></td></tr>
</table></div>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.cache.clean"></a>4.2.3. Cleaning the cache</h3></div></div></div>
<p>
            To remove/invalidate in particular cache id, you can use the <code class="code">remove()</code>
            method :
        </p>
<pre class="programlisting">&lt;?php
$cache-&gt;remove('idToRemove');
        </pre>
<p>
            To remove/invalidate several cache ids in one operation, you can use the <code class="code">clean()</code>
            method. For example to remove all cache records :
        </p>
<pre class="programlisting">&lt;?php
// clean all records
$cache-&gt;clean(Zend_Cache::CLEANING_MODE_ALL);

// clean only outdated
$cache-&gt;clean(Zend_Cache::CLEANING_MODE_OLD);
        </pre>
<p>
            If you want to remove cache entries matching the tags 'tagA' and 'tagC':
        </p>
<pre class="programlisting">&lt;?php
$cache-&gt;clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('tagA', 'tagC'));
        </pre>
<p>
            Available cleaning modes are: <code class="code">CLEANING_MODE_ALL</code>, <code class="code">CLEANING_MODE_OLD</code>,
            <code class="code">CLEANING_MODE_MATCHING_TAG</code> and <code class="code">CLEANING_MODE_NOT_MATCHING_TAG</code>. The
            latter are, as their names suggest, combined with an array of tags in cleaning operations.
        </p>
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.cache.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.cache.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.cache.frontends.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 4. Zend_Cache </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> 4.3. Zend_Cache frontends</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
